#include "core/regions/end_point_provider.h"

namespace aliyun {

std::vector<Endpoint> EndpointProvider::endpoints_;

void EndpointProvider::SetDefaultEndpoints(void) {
  std::vector<string> region_ids;
  region_ids.push_back("cn-hangzhou");
  region_ids.push_back("cn-qingdao");
  region_ids.push_back("cn-hongkong");
  region_ids.push_back("cn-shanghai");
  region_ids.push_back("us-west-1");
  region_ids.push_back("cn-shenzhen");
  region_ids.push_back("ap-southeast-1");

  std::vector<ProductDomain> product_domains;
  product_domains.push_back(ProductDomain("Ecs", "ecs.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Rds", "rds.aliyuncs.com"));
  product_domains.push_back(ProductDomain("BatchCompute", "batchCompute.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Bss", "bss.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Oms", "oms.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Slb", "slb.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Oss", "oss-cn-hangzhou.aliyuncs.com"));
  product_domains.push_back(ProductDomain("OssAdmin", "oss-admin.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Sts", "sts.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Yundun", "yundun-cn-hangzhou.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Risk", "risk-cn-hangzhou.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Drds", "drds.aliyuncs.com"));
  product_domains.push_back(ProductDomain("M-kvstore", "m-kvstore.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Ram", "ram2.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Cms", "metrics.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Crm", "crm-cn-hangzhou.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Ocs", "pop-ocs.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Ots", "ots-pop.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Dqs", "dqs.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Location", "location.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Ubsms", "ubsms.aliyuncs.com"));
  product_domains.push_back(ProductDomain("Ubsms-inner", "ubsms-inner.aliyuncs.com"));

  endpoints_.clear();
  endpoints_.push_back(Endpoint("cn-hangzhou", region_ids, product_domains));
}

string EndpointProvider::FindProductDomain(const string& region_id, const string& product) {
  if(region_id.empty() || product.empty() || endpoints_.size() == 0) {
    return "";
  }
  for(std::vector<Endpoint>::const_iterator iter_end_point = endpoints_.begin(); iter_end_point != endpoints_.end(); iter_end_point++) {
    const std::vector<string>& region_ids = iter_end_point->GetRegionIds();
    for(std::vector<string>::const_iterator iter_region_id = region_ids.begin(); iter_region_id != region_ids.end(); iter_region_id++) {
      if(region_id == *iter_region_id) {
        const std::vector<ProductDomain>& product_domains = iter_end_point->GetProductDomains();
        for(std::vector<ProductDomain>::const_iterator iter_producy_domain = product_domains.begin(); iter_producy_domain != product_domains.end(); iter_producy_domain++) {
          if(product == iter_producy_domain->GetProductName()) {
            return iter_producy_domain->GetDomainName();
          }
        }
      }
    }
  }
  return "";
}

std::vector<Endpoint>& EndpointProvider::GetEndpoints(void) {
  return endpoints_;
}
void EndpointProvider::SetEndpoints(const std::vector<Endpoint>& endpoints) {
  endpoints_ = endpoints;
}

} // namespace aliyun {

// end of file
